<template>
  <PageWrapper dense contentFullHeight fixedHeight contentClass="flex">
    <feed-type-tree class="w-1/4 xl:w-1/5" @select="handleSelect" />
    <BasicTable @register="registerTable" class="w-3/4 xl:w-4/5" :searchInfo="searchInfo">
      <template #firstImgUrl="{ text }">
        <TableImg :size="60" :simpleShow="true" :imgList="[text]" />
      </template>
      <template #toolbar>
        <a-button type="primary" @click="handAddDrawer">新增菜品</a-button>
      </template>
      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              label: '编辑',
              title: '编辑',
              onClick: handUnitEditDrawer.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <PracticeDrawer @register="registerDrawer" @success="handleSuccess" />
  </PageWrapper>
</template>

<script lang="ts">
  import { defineComponent, ref, reactive } from 'vue';
  import { BasicTable, TableAction, useTable, TableImg } from '/@/components/Table';
  import { PageWrapper } from '/@/components/Page';
  import { columns, searchFormSchema } from './index.data';
  import FeedTypeTree from './components/FeedTypeTree/FeedTypeTree.vue';
  import PracticeDrawer from './modules/CuisineAddDrawer.vue';
  import { useDrawer } from '/@/components/Drawer';
  import { getListPage } from '/@/api/cuisine/cuisine';

  export default defineComponent({
    name: 'Index',
    components: {
      BasicTable,
      PageWrapper,
      TableAction,
      FeedTypeTree,
      PracticeDrawer,
      TableImg,
    },
    setup() {
      const searchInfo = reactive<any>({});

      function handleSelect(pid: number) {
        searchInfo.cuisineTypeId = pid;
        reload();
      }

      const [registerDrawer, { openDrawer }] = useDrawer();

      function handAddDrawer() {
        openDrawer(true, {
          isUpdate: false,
        });
      }

      function handEditDrawer(record) {
        openDrawer(true, {
          isUpdate: true,
          record,
        });
      }

      function handleSuccess() {
        reload();
      }

      const [registerTable, { reload }] = useTable({
        title: '菜品列表',
        rowKey: 'id',
        api: getListPage,
        columns,
        dataSource: [],
        formConfig: {
          labelWidth: 120,
          schemas: searchFormSchema,
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        handleSearchInfoFn(info) {
          info.type = 2;
          return info;
        },
        actionColumn: {
          width: 120,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
        },
      });

      return {
        handAddDrawer,
        handleSelect,
        searchInfo,
        registerTable,
        registerDrawer,
        handUnitEditDrawer: handEditDrawer,
        handleSuccess,
      };
    },
  });
</script>

<style scoped></style>
